home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1012 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.6 KB

  1. From: kanze@gabi-soft.fr (J. Kanze)
  2. Message-ID: <KANZE.96Apr9123729@gabi.gabi-soft.fr>
  3. X-Original-Date: 09 Apr 1996 10:37:29 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 09 Apr 96 13:22:25 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: C++ syntactic trap
  9. Organization: GABI Software, Sarl.
  10. References: <4k3q4p$lkd@syn.cs.cornell.edu>
  11. In-Reply-To: vavasis@CS.Cornell.EDU's message of 06 Apr 1996 11:33:39 PST
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMWpkmOEDnX0m9pzZAQG+AgF+PNwlgMZRMfjM4OJ1rVKqLSsvZAYPJTII
  14.     cRFVXAz4Mq8YOdHhraWJQysNe+/Fn2AP
  15.     =MwjO
  16.  
  17. In article <4k3q4p$lkd@syn.cs.cornell.edu> vavasis@CS.Cornell.EDU
  18. (Stephen Vavasis) writes:
  19.  
  20. |> I have just spent a long time tracking down a mysterious bug (the
  21. |> heap-trashing variety of bug) in my program caused by a syntactic trap
  22. |> in C++.  The troubling thing about this trap is that none of the unix
  23. |> compilers I tried (gcc-2.7.2, Sun SC3.0.1, HP-UX cfront 3.0.3) issued
  24. |> a warning about the mistake, even on the highest warning level.  Only
  25. |> Visual C++4.0 observed that there might be a problem.  Here is an
  26. |> example of the trap.  This program asks the user how many asterisks,
  27. |> and then prints out that many asterisks.
  28.  
  29. |> #include <iostream.h>
  30. |> int main() {
  31. |>   cout << " How many *'s? ";
  32. |>   int sz; cin >> sz;
  33. |>   char* a = new char(sz + 1); // bug is here, but syntax is legal.
  34. |>   for (int i = 0; i < sz; i++)
  35. |>     a[i] = '*';
  36. |>   a[sz] = 0;
  37. |>   cout << a << endl;
  38. |>   delete[] a;
  39. |>   return 0;
  40. |> }
  41.  
  42. |> (Does everyone see the error?  I did not, even after staring at my
  43. |> code for a long time.  The point is that the marked statement is an
  44. |> unintended cast from int to char because I used () instead of [].)
  45.  
  46. |> I would like to make a plea to the compiler-writers who read this
  47. |> group: please issue warnings for syntactic trouble spots!  Implicit
  48. |> type conversion probably creates other traps that I haven't thought
  49. |> of.  C++ programmers like me need help from the compiler to navigate
  50. |> the traps!
  51.  
  52. The problem is that most of the time, it is the initialization of a
  53. single object (the form that you used) which is wanted.  In practice,
  54. one simply doesn't allocate arrays.  Thus, your program would typically
  55. be more like:
  56.  
  57.     int
  58.     main()
  59.     {
  60.         int             results( EXIT_FAILURE ) ;
  61.         cout << " How many *'s? " << flush ;
  62.         int             sz ;
  63.         cin >> sz ;
  64.         if ( ! cin || sz < 0 )
  65.             cerr << "Illegal input value or EOF" << endl ;
  66.         else
  67.         {
  68.             cout << string( sz , '*" ) << endl ;
  69.             result = EXIT_SUCCESS ;
  70.         }
  71.         return results ;
  72.     }
  73.  
  74. Even if the purpose of the exercise is to use a loop to fill a vector,
  75. the else branch of the if would be something like:
  76.  
  77.     {
  78.         vector< char >      a( sz + 1 ) ;
  79.         //  ...
  80.     }
  81.  
  82. (Note that in this case, the input validation would have to reject the
  83. value of INT_MAX, as well as negative values, in order to avoid a
  84. possible overflow.)
  85. -- 
  86. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  87. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  88. Conseils en informatique industrielle --
  89.                             -- Beratung in industrieller Datenverarbeitung
  90. ---
  91. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  92. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  93. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  94. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  95. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  96.